import Vue from 'vue'
import VueRouter from 'vue-router'
import findLast from 'lodash/findLast'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Home from '../views/Home.vue'
import NotFound from '../views/404.vue'
import forbidden from '../views/403.vue'
import {check, isLogin} from '../utils/auth'
import nProgress from 'nprogress'
import {notification} from 'ant-design-vue'
 
Vue.use(VueRouter)

const routes = [
  {
    path: '/user',
    HideInMenu: true,
    component: () => import("../layouts/UserLayout.vue"),
    children: [
      {
        path: '/user',
        redirect: '/user/login'
      },
      {
        path: '/user/login',
        name: 'login',
        component: () => import("../views/User/Login.vue")
      },
      {
        path: '/user/register',
        name: 'register',
        component: () => import("../views/User/Register.vue")
      }
    ]
  },
  {
    path: '/',
    meta:{authority:["admin","guest"]},
    component: () => import("../layouts/BasicLayout"),
    children: [
      {
        path: "/",
        HideChildrenInMenu:true,
        redirect: "/dashboard/analysis"
      },
      {
        path: '/dashboard',
        name: "dashboard",
        meta: { icon: "dashboard", title: "仪表盘",authority:["admin","user"] },
        component: { render: h => h("router-view") },
        children: [{
          path: "/dashboard/analysis",
          name: "anaysis",
          meta: { icon: "dashboard",title: "分析页面",authority:["admin","user"]  },
          component: () => import("../views/DashBoard/Analysis")
        }]
      },
      {
        path: "/form",
        name: "form",
        meta: { icon: "form", title: "表单" ,authority:["admin"]},
        component: { render: h => h("router-view") },
        children: [{
          path: "/form/basic-form",
          name: "basicform",
          meta: { title: "基础表单",authority:["admin"] },
          component: () => import("../views/Form/BasicForm.vue")

        }]
      },
      {
        path: "/form/step-form",
        name: "stepform",
        meta: { icon: "dashboard",title: "分布表单",authority:["admin"] },
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/form/step-form",
            redirect: "/form/step-form/info"
          },
          {
            path: "/form/step-form/Info",
            name: "info",
            meta: { icon: "dashboard",title: "分布表单1",authority:["admin"]  },
            component: () => import("../views/Form/StepForm/Step1.vue")
          },
          {
            path: "/form/step-form/confirm",
            name: "confirm",
            meta: {icon: "dashboard", title: "分布表单2" },
            component: () => import("../views/Form/StepForm/Step2")
          },
          {
            path: "/form/step-form/result",
            name: "result",
            meta: {icon: "dashboard", title: "分布表单3" },
            component: () => import("../views/Form/StepForm/Step3")
          }
        ]
      }
    ]
  },
  {
    path: '/',
    name: 'Home',
    HideInMenu: true,
    component: Home
  },
  {
    path: '*',
    name: '404',
    HideInMenu: true,
    component: NotFound,
  },
  {
    path: '/403',
    name: '403',
    HideInMenu: true,
    component: forbidden
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, form, next) => {
  if (to.path != form.path) {
    NProgress.start();
  }
  const record=findLast(to.matched,record=>record.meta.authority);
  if(record&&!check(record.meta.authority))
  {
    if(!isLogin()&&to.path!='/user/login')
    {
      next({
        path:'/user/login'
      })
    }else if(to.path!=="/403")
    {
      notification.error({
        message: '403',
        description:
          '权限不足',
      });
        next({
          path:'/403'
        })
    }
    nProgress.done();
  }
  next();
})
router.afterEach(() => {
  NProgress.done();
})
export default router
